export async function post_with_json<T>(url: string, params: T): Promise<any> {
  // eslint-disable-next-line no-useless-catch
  try {
    const response = await fetch(url, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        Authorization: `${localStorage.getItem('token') !== null ? `Bearer ${localStorage.getItem('token')}` : null}`,
      },
      credentials: 'include',
      body: JSON.stringify(params), // 将对象序列化为 JSON 字符串
    });
    if (response.status === 401 || response.status === 403) {
      // 401 未认证，跳转到登录页面
      localStorage.removeItem('isAuthenticated');
      localStorage.removeItem('token');
      window.location.href = '/sign-in'; // 或使用 router.push('/sign-in')，根据你的路由方式调整
      return;
    }
    // eslint-disable-next-line consistent-return
    return await response.json(); // 返回解析后的 JSON 数据
  } catch (e) {
    throw e; // 将错误抛出，让调用者处理
  }
}

export async function delete_with_param(url: string): Promise<any> {
  // eslint-disable-next-line no-useless-catch
  try {
    const response = await fetch(url, {
      method: 'DELETE',
      headers: {
        Authorization: `${localStorage.getItem('token') !== null ? `Bearer ${localStorage.getItem('token')}` : null}`,
      },
      credentials: 'include',
    });
    if (response.status === 401 || response.status === 403) {
      // 401 未认证，跳转到登录页面
      localStorage.removeItem('isAuthenticated');
      localStorage.removeItem('token');
      window.location.href = '/sign-in'; // 或使用 router.push('/sign-in')，根据你的路由方式调整
      return;
    }
    // eslint-disable-next-line consistent-return
    return await response.json(); // 返回解析后的 JSON 数据
  } catch (e) {
    throw e; // 将错误抛出，让调用者处理
  }
}

export async function put_with_json<T>(url: string, params: T): Promise<any> {
  // eslint-disable-next-line no-useless-catch
  try {
    const response = await fetch(url, {
      method: 'PUT',
      headers: {
        'Content-Type': 'application/json',
        Authorization: `${localStorage.getItem('token') !== null ? `Bearer ${localStorage.getItem('token')}` : null}`,
      },
      credentials: 'include',
      body: JSON.stringify(params), // 将对象序列化为 JSON 字符串
    });
    if (response.status === 401 || response.status === 403) {
      // 401 未认证，跳转到登录页面
      localStorage.removeItem('isAuthenticated');
      localStorage.removeItem('token');
      window.location.href = '/sign-in'; // 或使用 router.push('/sign-in')，根据你的路由方式调整
      return;
    }
    // eslint-disable-next-line consistent-return
    return await response.json(); // 返回解析后的 JSON 数据
  } catch (e) {
    throw e; // 将错误抛出，让调用者处理
  }
}

export async function get_without_param(url: string): Promise<any> {
  try {
    const response = await fetch(url, {
      method: 'GET',
      headers: {
        Authorization: `${localStorage.getItem('token') !== null ? `Bearer ${localStorage.getItem('token')}` : null}`,
      },
      credentials: 'include',
    });

    if (response.status === 401 || response.status === 403) {
      // 401 未认证，跳转到登录页面
      window.location.href = '/sign-in'; // 或使用 router.push('/sign-in')，根据你的路由方式调整
      localStorage.removeItem('isAuthenticated');
      localStorage.removeItem('token');
      return;
    }
    const rest = response.json();
    // eslint-disable-next-line consistent-return
    return await rest;
  } catch (e) {
    console.error('Error fetching data:', e);
    throw e; // 将错误抛出，让调用者决定如何处理
  }
}

export async function post_without_params(url: string): Promise<any> {
  // eslint-disable-next-line no-useless-catch
  try {
    const response = await fetch(url, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        Authorization: `${localStorage.getItem('token') !== null ? `Bearer ${localStorage.getItem('token')}` : null}`,
      },
      credentials: 'include',
    });

    if (!response.ok) {
      throw new Error(`HTTP error! Status: ${response.status}`);
    }
    return await response.json(); // 返回解析后的 JSON 数据
  } catch (e) {
    throw e; // 将错误抛出，让调用者处理
  }
}
